package huanzhao.erp.service;

import cn.hutool.core.bean.BeanUtil;
import huanzhao.erp.domain.entity.*;
import huanzhao.erp.domain.types.NotificationType;
import huanzhao.erp.domain.vo.*;
import huanzhao.erp.mapper.*;
import org.springframework.stereotype.Service;

/**
 * @author April
 * 2024/10/2 22:33
 * @version 1.0
 */
@Service
public class ConvertService {
    private final NotificationMapper notificationMapper;
    private final PhoneMapper phoneMapper;
    private final TaskMapper taskMapper;
    private final ExceptionMapper exceptionMapper;
    private final FinanceSettlementMapper financeSettlementMapper;
    private final StaffMapper staffMapper;
    private final TeamMapper teamMapper;
    private final PhoneDetailMapper phoneDetailMapper;
    private final ExternalAccountMapper externalAccountMapper;

    public ConvertService(NotificationMapper notificationMapper, PhoneMapper phoneMapper, TaskMapper taskMapper, ExceptionMapper exceptionMapper, FinanceSettlementMapper financeSettlementMapper, StaffMapper staffMapper, TeamMapper teamMapper, PhoneDetailMapper phoneDetailMapper, ExternalAccountMapper externalAccountMapper) {
        this.notificationMapper = notificationMapper;
        this.phoneMapper = phoneMapper;
        this.taskMapper = taskMapper;
        this.exceptionMapper = exceptionMapper;
        this.financeSettlementMapper = financeSettlementMapper;
        this.staffMapper = staffMapper;
        this.teamMapper = teamMapper;
        this.phoneDetailMapper = phoneDetailMapper;
        this.externalAccountMapper = externalAccountMapper;
    }

    /**
     * 将从数据库中查询到的通知对象转换为VO对象
     *
     * @param notification 通知对象
     * @return NotificationVO
     * 2024/10/2 20:31:22
     */
    public NotificationVO convertNotification(Notification notification) {
        Long coreInfoId = notification.getCoreInfoId();
        // 获取通知核心信息
        NotificationCore coreInfo = notificationMapper.getNotificationCoreInfoById(coreInfoId);
        // 整合通知核心信息和通知信息到VO中
        NotificationVO notificationVO = BeanUtil.copyProperties(notification, NotificationVO.class);
        BeanUtil.copyProperties(coreInfo, notificationVO);
        // 根据通知类型设置相关对象
        fillNotificationVO(notificationVO, coreInfo.getRelateId());
        return notificationVO;
    }

    /**
     * 根据通知类型设置相关对象
     *
     * @param notificationVO 通知对象
     *                       2024/6/22 下午1:30:52
     */
    public void fillNotificationVO(NotificationVO notificationVO, long relateId) {
        NotificationType notificationType = notificationVO.getType();
        switch (notificationType) {
            case NEW_PHONE, PHONE_TIMEOUT:
                notificationVO.setPhone(BeanUtil.copyProperties(phoneMapper.selectById(relateId), PhoneVO.class));
                break;
            case NEW_TASK, TASK_TIMEOUT:
                notificationVO.setTask(BeanUtil.copyProperties(taskMapper.selectById(relateId), TaskVO.class));
                break;
            case NEW_EXCEPTION, EXCEPTION_REPLY:
                notificationVO.setException(BeanUtil.copyProperties(exceptionMapper.selectById(relateId), ExceptionVO.class));
                break;
            case FINANCE_SETTLE:
                notificationVO.setFinanceSettlement(BeanUtil.copyProperties(financeSettlementMapper.selectById(relateId), FinanceSettlementVO.class));
                break;
            default:
                break;
        }
    }

    public TaskVO convertTask(Task task) {
        TaskVO taskVO = BeanUtil.copyProperties(task, TaskVO.class);
        taskVO.setPhone(convertPhone(phoneMapper.selectById(task.getPhoneId())));
        taskVO.setStaff(convertStaff(staffMapper.selectById(task.getReceiverId())));
        taskVO.setTeam(convertTeam(teamMapper.getTeamById(task.getTeamId())));
        return taskVO;
    }

    public PhoneVO convertPhone(Phone phone) {
        PhoneVO phoneVO = BeanUtil.copyProperties(phone, PhoneVO.class);
        // 获取手机详情
        PhoneDetail phoneDetail = phoneDetailMapper.getPhoneDetailByPhoneId(phone.getId());
        phoneVO.setExtra(convertPhoneDetail(phoneDetail));
        return phoneVO;
    }

    public PhoneDetailVO convertPhoneDetail(PhoneDetail phoneDetail) {
        PhoneDetailVO phoneDetailVO = BeanUtil.copyProperties(phoneDetail, PhoneDetailVO.class);
        phoneDetailVO.setPurchaser(staffMapper.getNameById(phoneDetail.getPurchaserId()));
        phoneDetailVO.setExternalAccount(convertExternalAccount(externalAccountMapper.selectById(phoneDetail.getExternalAccountId())));
        // todo: 可能需要添加手机上架、售出信息字段
        return phoneDetailVO;
    }

    public ExternalAccountVO convertExternalAccount(ExternalAccount externalAccount) {
        ExternalAccountVO externalAccountVO = BeanUtil.copyProperties(externalAccount, ExternalAccountVO.class);
        // todo: set other fields
        return externalAccountVO;
    }

    public StaffVO convertStaff(Staff staff) {
        return BeanUtil.copyProperties(staff, StaffVO.class);
    }

    public TeamVO convertTeam(Team team) {
        TeamVO teamVO = BeanUtil.copyProperties(team, TeamVO.class);
        teamVO.setManager(staffMapper.getNameById(team.getManagerId()));
        return teamVO;
    }


    public PositionVO convertPosition(Position position) {
        PositionVO positionVO = BeanUtil.copyProperties(position, PositionVO.class);
        positionVO.setTeam(convertTeam(teamMapper.getTeamById(position.getTeamId())));
        return positionVO;
    }

    public GlobalSettingVO convertGlobalSetting(GlobalSetting globalSetting) {
        GlobalSettingVO globalSettingVO = new GlobalSettingVO();
        globalSettingVO.setLimit(globalSetting.getLimit());
        globalSettingVO.setManager(staffMapper.getNameById(globalSetting.getManagerId()));
        globalSettingVO.setFinancer(staffMapper.getNameById(globalSetting.getFinancerId()));
        globalSettingVO.setRepairLeader(staffMapper.getNameById(globalSetting.getRepairLeaderId()));
        globalSettingVO.setWarehouseHandler(staffMapper.getNameById(globalSetting.getWarehouseHandlerId()));
        globalSettingVO.setPurchaseHandler(staffMapper.getNameById(globalSetting.getPurchaseHandlerId()));
        return globalSettingVO;
    }

    public LabelVO convertLabel(Label label) {
        LabelVO labelVO = BeanUtil.copyProperties(label, LabelVO.class);
        labelVO.setModifier(staffMapper.getNameById(label.getModifierId()));
        return labelVO;
    }

    public ClearVO convertClear(Clear clear) {
        return BeanUtil.copyProperties(clear, ClearVO.class);
    }

    public ClearRecordVO convertClearRecord(ClearRecord clearRecord) {
        return BeanUtil.copyProperties(clearRecord, ClearRecordVO.class);
    }
}
